from eventServer import eventServer
from gameServer import gameServer
from tCube import TCube

class ItemBase(TCube):
    
    def __init__(self):
        TCube.__init__(self)
        
        self.setVar("itemPlayer", None)
        self.setVar("base", "item")
        eventServer.register("stopLevel", self.stopLevel)
        
        self.setVar("timedUse", False)
        self.setVar("lifeSpan", 5)
        self.setVar("lifeBlinkTime", 1)
        
        self.setVar("timedCollect", False)
        self.setVar("collectTime", 10)
        self.setVar("collectBlinkTime", 2)
        
        self.setVar("mass", 1.0)
        self.setVar("frictionImmune", False)
        self.setVar("elasticity", 0.6)
        
    def stopLevel(self, cmd, args):
        gameServer.removeObject(self.getVar("name"))
        
    def useTimeExpired(self, player):
        pass
    
    def itemCollision(self, player):
        pass
    
    def onPickUp(self, player):
        pass
        
    def update(self, cmd, args):
        TCube.update(self, cmd, args)
        
        player = self.getVar("itemPlayer")
        if(not(player == None)):
            self.blinkCube(args['dtime'])
            
            if(self.getVar("timedUse")):
                self.setVar("lifeSpan", self.getVar("lifeSpan") - args['dtime'])
                l = self.getVar("lifeSpan")
                if(l < self.getVar("lifeBlinkTime")):
                    self.blink = True
                if(l < 0):
                    self.useTimeExpired(self.getVar("itemPlayer").getVar("player"))
        else:
            if(self.getVar("timedCollect")):
                self.setVar("collectTime", self.getVar("collectTime") - args['dtime'])
                l = self.getVar("collectTime")
                if(l < self.getVar("collectBlinkTime")):
                    self.blink = True
                if(l < 0):
                    gameServer.removeObject(self.getVar("name"))
                    
    def onCollision(self, dist, vec, side, sideType, above, toward, ignoreSurface, object):
        TCube.onCollision(self, dist, vec, side, sideType, above, toward, ignoreSurface, object)
        
        if(object.hasVar("base")):
            b = object.getVar("base")
            player = self.getVar("itemPlayer")
            if(player == None):
                if(b == "player"):
                    itemName  = self.getVar("name")
                    playerNum = object.getVar("player")
                    eventServer.pull('onItemCollide',{'player':playerNum, 'item':itemName})
                    self.itemCollision(object)